home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / wstype / source / backg.c < prev    next >
C/C++ Source or Header  |  1991-10-18  |  3KB  |  180 lines

  1. /***   [backg.c]
  2. *
  3. *    背景画 関連        (C)ささがわ
  4. *
  5. *    For GNU C Compiler (GCC)   Version 1.39
  6. *
  7. ***/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "graph.h"
  13. #include "optparse.h"
  14. #include "others.h"
  15. #include "icn.h"
  16.  
  17. extern int    PAL_Black;
  18. static unsigned char    bgp[153600];
  19.  
  20. static void    beta(int);
  21. static long    readN(FILE *, int);
  22. static int    Tiff(char *);
  23. static int    Tile(char *, int);
  24. static void    transfill(void);
  25.  
  26. void BACKG_init(void) {
  27.     int        no;
  28.     char    path[90];
  29.     
  30.     ICN_mos(1);
  31.     switch (OPT_BackG(path, &no)) {
  32.         case 0:
  33.             if (Tiff(path)) {
  34.                 WIND_error(3, path);
  35.                 beta(PAL_Black);
  36.             }
  37.             break;
  38.         case 1:
  39.             if (Tile(path, no)) {
  40.                 WIND_error(3, path);
  41.                 beta(PAL_Black);
  42.             }
  43.             break;
  44.         default:
  45.             if (1 <= no && no <= 15)
  46.                 beta(no);
  47.             else
  48.                 beta(PAL_Black);
  49.             break;
  50.     }
  51.     ICN_mos(0);
  52. }
  53.  
  54. static void beta(int c) {
  55.     memset(bgp, (c << 4) + c, 153600);
  56. }
  57.  
  58. void BACKG_draw(int x, int y) {
  59.     EGB_put(3, bgp, x, y, x + 639, y + 479);
  60. }
  61.  
  62. static int Tiff(char *path) {
  63.     int        i, ifdn, xx = 0, yy = 0, bp = 0;
  64.     long    ifdoff, dataoff = 0;
  65.     FILE    *fp;
  66.     
  67.     if ((fp = fopen(path, "rb")) == NULL)
  68.         return -1;
  69.     
  70.     clearerr(fp);
  71.     if (fgetc(fp) != 0x49 || fgetc(fp) != 0x49 || fgetc(fp) != 0x2a || fgetc(fp) != 0x00) {
  72.         fclose(fp);
  73.         return -1;
  74.     }
  75.     
  76.     if ((ifdoff = readN(fp, 4)) < 0) {
  77.         fclose(fp);
  78.         return -1;
  79.     }
  80.     
  81.     fseek(fp, ifdoff, SEEK_SET);
  82.     if ((ifdn = readN(fp, 2)) < 0) {
  83.         fclose(fp);
  84.         return -1;
  85.     }
  86.     
  87.     for (i = 0; i < ifdn; i++) {
  88.         int        tag, a;
  89.         
  90.         tag = readN(fp, 2);
  91.         fseek(fp, 6l, SEEK_CUR);
  92.         a = readN(fp, 4);
  93.         switch (tag) {
  94.             case 0x0100:    xx = a;    break;
  95.             case 0x0101:    yy = a;    break;
  96.             case 0x0102:    bp = a;    break;
  97.             case 0x0111:    dataoff = a;    break;
  98.         }
  99.     }
  100.     if (xx < 1 || 640 < xx || yy < 1 || bp != 4 || dataoff < 0) {
  101.         fclose(fp);
  102.         return -1;
  103.     }
  104.     
  105.     fseek(fp, dataoff, SEEK_SET);
  106.     memset(bgp, 0, 153600);
  107.     if (yy > 480)
  108.         yy = 480;
  109.     for (i = 0; i < yy; i++) {
  110.         fread(bgp + i * 320, 1, (xx + 1) / 2, fp);
  111.         if (ferror(fp) || feof(fp)) {
  112.             fclose(fp);
  113.             return -1;
  114.         }
  115.     }
  116.     fclose(fp);
  117.     transfill();
  118.     
  119.     return 0;
  120. }
  121.  
  122. static int Tile(char *path, int n) {
  123.     int        x, y;
  124.     FILE    *fp;
  125.     
  126.     if ((fp = fopen(path, "rb")) == NULL)
  127.         return -1;
  128.     
  129.     clearerr(fp);
  130.     fseek(fp, n * 512, SEEK_SET);
  131.     for (y = 0; y < 32; y++) {
  132.         fread(bgp + y * 320, 1, 16, fp);
  133.         if (ferror(fp) || feof(fp)) {
  134.             fclose(fp);
  135.             return -1;
  136.         }
  137.     }
  138.     fclose(fp);
  139.     
  140.     for (y = 0; y < 15; y++) {
  141.         for (x = 0; x < 20; x++) {
  142.             int        i;
  143.             
  144.             if (x == 0 && y == 0)
  145.                 continue;
  146.             
  147.             for (i = 0; i < 32; i++)
  148.                 memcpy(bgp + y * 10240 + i * 320 + x * 16, bgp + i * 320, 16);
  149.         }
  150.     }
  151.     transfill();
  152.     
  153.     return 0;
  154. }
  155.  
  156. static long readN(FILE *fp, int n) {
  157.     int        i, ret;
  158.     
  159.     for (ret = 0, i = 0; i < n; i++) {
  160.         ret += fgetc(fp) << i * 8;
  161.         if (ferror(fp) || feof(fp)) {
  162.             ret = -1;
  163.             break;
  164.         }
  165.     }
  166.     
  167.     return ret;
  168. }
  169.  
  170. static void transfill(void) {
  171.     int        i;
  172.     
  173.     for (i = 0; i < 153600; i++) {
  174.         if ((bgp[i] & 0xf0) == 0)
  175.             bgp[i] = (bgp[i] & 0x0f) + (PAL_Black << 4);
  176.         if ((bgp[i] & 0x0f) == 0)
  177.             bgp[i] = (bgp[i] & 0xf0) + PAL_Black;
  178.     }
  179. }
  180.